import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap } from './router.config'

Vue.use(Router)

// 不需要登录拦截的路由配置
const loginIgnore = {
  names: ['404', '403','login','register'],      //根据路由名称匹配
  paths: [],
  /**
   * 判断路由是否包含在该配置中
   * @param route vue-router 的 route 对象
   * @returns {boolean}
   */
  includes(route) {
    return this.names.includes(route.name) || this.paths.includes(route.path)
  }
}
// hack router push callback vue-router跳转相同路径报错
const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
/**
 * 初始化路由实例
 * @returns {VueRouter}
 */
function initRouter() {
  const options = {}
  options.routes=constantRouterMap
  options.mode='history'
  return new Router(options)
}
export {loginIgnore, initRouter}
